package objectio;

import java.io.*;

public class TestDriver {
    public static void main(String[] args) {
        System.out.println("=======================");
        test01();
    }

    /**
     * 对象流和序列化、transient关键字
     */
    public static void test01() {
        // 在内存中创建一个Teacher的实例对象
        Teacher teacher = new Teacher();
        // 设值
        teacher.setTeaName("张三");
        teacher.setTeaNo("10010");
        teacher.setTeaSore(90);

        try {
            // 在这个时间阶段，将teacher对象的内存信息保存到文件对象中
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    new FileOutputStream("D:/temp/filesdemo/tea001.dat"));
            // 写出对象
            objectOutputStream.writeObject(teacher);

            System.out.println("将teacher对象从内存中写出到文件了");
            System.out.println(teacher);
            System.out.println("-----------------------------");
            ObjectInputStream objectInputStream = new ObjectInputStream(
                    new FileInputStream("D:/temp/filesdemo/tea001.dat"));
            // 读入对象流
            Object objIn = objectInputStream.readObject();
            System.out.println("读入了对象流：");
            System.out.println(objIn);

            // 写出的teacher对象和读入对象流对象是同一个对象
            System.out.println("teacher == objIn:" + (teacher == objIn));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
